package com.freegeese.security.test.chapter2.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * 认证域
 */
public class CustomRealm2 implements Realm {
    @Override
    public String getName() {
        return CustomRealm2.class.getSimpleName();
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        return UsernamePasswordToken.class.isAssignableFrom(token.getClass());
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        // 主体
        String principal = String.valueOf(token.getPrincipal());
        // 凭证
        String credentials = String.valueOf((char[]) token.getCredentials());
        if (!"b".equals(principal)) {
            throw new UnknownAccountException();
        }
        if (!"2".equals(credentials)) {
            throw new IncorrectCredentialsException();
        }
        return new SimpleAuthenticationInfo(principal+"@qq.com", credentials, getName());
    }
}
